package com.enjoy.xbase.tools;

import android.content.Context;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class AssetsTools {

    public static StringBuffer getAssetsText(Context context, String name) {
        InputStream in = null;
        BufferedReader bufferedReader = null;
        try {
            in = context.getAssets().open(name);
            bufferedReader = new BufferedReader(new InputStreamReader(in));
            StringBuffer buffer = new StringBuffer();
            int lenght = 0;
            char[] temp = new char[512];
            while ((lenght = bufferedReader.read(temp)) != -1) {
                buffer.append(temp, 0, lenght);
            }
            return buffer;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
